package View.dianming;

import Service.BreakRuleService;
import Service.DormService;
import Service.OutApplyService;
import Service.ServiceImpl.BreakRuleServiceImpl;
import Service.ServiceImpl.DormServiceImpl;
import Service.ServiceImpl.OutApplyServiceImpl;
import Service.ServiceImpl.stustateServiceImpl;
import Service.stustateService;
import View.Index.Index_admin;
import View.apply.OutApplyApprove;
import View.apply.OutApplyPage;
import com.sun.jdi.event.ThreadStartEvent;
import domain.*;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;
import java.util.Vector;

public class show extends JFrame {
    public show(User user,String dormnum){
        init();
        componentInit(user,dormnum);
    }

    private Vector titlesv= new Vector();
    private Vector<Vector> datev= new Vector<>();
    JTable table=null;
    public void init(){
        this.setTitle("宿舍学生状态详情");
        this.setSize(800,500);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user,String dormnum){
        String[] title = {"姓名","学号","是否在宿舍"};

        JButton button1 = new JButton();
        button1.setText("进行点名");
        button1.setBounds(100, 340, 175, 43);

        JButton button2 = new JButton();
        button2.setText("退出");
        button2.setBounds(500, 340, 175, 43);

        JScrollPane panel = new JScrollPane();
        panel.setBounds(100, 5, 590, 300);

        DormService service = new DormServiceImpl();
        Dorm dorm = service.selectDormByDormnum(dormnum);

        stustateService service1 = new stustateServiceImpl();
        stustate stustate1 = service1.selectStutateByName(dorm.getMember1());
        stustate stustate2 = service1.selectStutateByName(dorm.getMember2());
        stustate stustate3 = service1.selectStutateByName(dorm.getMember3());
        stustate stustate4 = service1.selectStutateByName(dorm.getMember4());
        stustate stustate5 = service1.selectStutateByName(dorm.getMember5());
        stustate stustate6 = service1.selectStutateByName(dorm.getMember6());

        Object[][] neirong = new Object[6][3];
        neirong[0][0]=dorm.getMember1();
        neirong[0][1]=stustate1.getStunum();
        neirong[0][2]=stustate1.getIsindorm();

        neirong[1][0]=dorm.getMember2();
        neirong[1][1]=stustate2.getStunum();
        neirong[1][2]=stustate2.getIsindorm();

        neirong[2][0]=dorm.getMember3();
        neirong[2][1]=stustate3.getStunum();
        neirong[2][2]=stustate3.getIsindorm();

        neirong[3][0]=dorm.getMember4();
        neirong[3][1]=stustate4.getStunum();
        neirong[3][2]=stustate4.getIsindorm();

        neirong[4][0]=dorm.getMember5();
        neirong[4][1]=stustate5.getStunum();
        neirong[4][2]=stustate5.getIsindorm();

        neirong[5][0]=dorm.getMember6();
        neirong[5][1]=stustate6.getStunum();
        neirong[5][2]=stustate6.getIsindorm();

        for (int i = 0; i < title.length; i++) {
            titlesv.add(title[i]);
        }

        for (int i = 0; i < neirong.length; i++) {
            Vector t = new Vector<>();
            for (int j = 0; j < neirong[i].length; j++) {
                t.add(neirong[i][j]);
            }
            datev.add(t);
        }
        MyTableModel tableModel = new MyTableModel();
        table = new JTable(tableModel);
        table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(SwingConstants.CENTER);
        table.setDefaultRenderer(Object.class, tcr);
        panel.setViewportView(table);

        this.add(panel);
        this.add(button1);
        this.add(button2);

        button1.addActionListener(new ActionListener() {
            BreakRule rule = new BreakRule();
            BreakRuleService service = new BreakRuleServiceImpl();
            @Override
            public void actionPerformed(ActionEvent e) {
                if(stustate1.getIsindorm().equals("false")){
                    rule.setStunum(stustate1.getStunum());
                    rule.setName(stustate1.getName());
                    rule.setDisReason("点名没到");
                    rule.setWriteTime(new Date());

                    service.addBreakRuleDao(rule);
                }
                if(stustate2.getIsindorm().equals("false")){
                    rule.setStunum(stustate2.getStunum());
                    rule.setName(stustate2.getName());
                    rule.setDisReason("点名没到");
                    rule.setWriteTime(new Date());

                    service.addBreakRuleDao(rule);
                }
                if(stustate3.getIsindorm().equals("false")){
                    rule.setStunum(stustate3.getStunum());
                    rule.setName(stustate3.getName());
                    rule.setDisReason("点名没到");
                    rule.setWriteTime(new Date());

                    service.addBreakRuleDao(rule);
                }

                if(stustate4.getIsindorm().equals("false")){
                    rule.setStunum(stustate4.getStunum());
                    rule.setName(stustate4.getName());
                    rule.setDisReason("点名没到");
                    rule.setWriteTime(new Date());

                    service.addBreakRuleDao(rule);
                }
                if(stustate5.getIsindorm().equals("false")){
                    rule.setStunum(stustate5.getStunum());
                    rule.setName(stustate5.getName());
                    rule.setDisReason("点名没到");
                    rule.setWriteTime(new Date());

                    service.addBreakRuleDao(rule);
                }
                if(stustate6.getIsindorm().equals("false")){
                    rule.setStunum(stustate6.getStunum());
                    rule.setName(stustate6.getName());
                    rule.setDisReason("点名没到");
                    rule.setWriteTime(new Date());

                    service.addBreakRuleDao(rule);
                }
                JOptionPane.showMessageDialog(new JFrame(),"已进行宿舍点名,未到宿舍的同学被计入违纪");
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new dianming(user).setVisible(true);
            }
        });
    }

    private class MyTableModel extends AbstractTableModel {

        @Override
        public int getRowCount() {
            return datev.size();
        }

        @Override
        public int getColumnCount() {
            return titlesv.size();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return datev.get(rowIndex).get(columnIndex);
        }

        @Override
        public String getColumnName(int column) {
            return (String) titlesv.get(column);
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }
    }
}
